##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.ui.preferences.preferences_widget import PreferencesWidget
import math

MIN_dB = 0.000000312  # -100dB


class Volume(PreferencesWidget):

    NAME = 'Volume'

    def __init__(self, size, parent=None):
        super(Volume, self).__init__(size, parent)

        self.verticalLayoutWidget_3 = QtGui.QWidget(self)
        self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(0, 0, 431, 121))
        self.verticalLayout_3 = QtGui.QVBoxLayout(self.verticalLayoutWidget_3)
        self.verticalLayout_3.setMargin(0)

        self.groupBox_10 = QtGui.QGroupBox(self.verticalLayoutWidget_3)

        self.horizontalLayoutWidget_4 = QtGui.QWidget(self.groupBox_10)
        self.horizontalLayoutWidget_4.setGeometry(QtCore.QRect(0, 20, 431, 60))
        self.horizontalLayout_4 = QtGui.QHBoxLayout(self.horizontalLayoutWidget_4)
        self.horizontalLayout_4.setMargin(0)

        self.horizontalSlider = QtGui.QSlider(self.horizontalLayoutWidget_4)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        self.horizontalSlider.setSizePolicy(sizePolicy)
        self.horizontalSlider.setMinimum(-1000)
        self.horizontalSlider.setMaximum(0)
        self.horizontalSlider.setPageStep(1)
        self.horizontalSlider.setProperty("value", 0)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setTickPosition(QtGui.QSlider.TicksAbove)
        self.horizontalSlider.setTickInterval(100)
        self.horizontalLayout_4.addWidget(self.horizontalSlider)

        self.label_7 = QtGui.QLabel(self.horizontalLayoutWidget_4)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        self.label_7.setSizePolicy(sizePolicy)
        self.label_7.setAlignment(QtCore.Qt.AlignCenter)
        self.horizontalLayout_4.addWidget(self.label_7)

        self.checkBox_4 = QtGui.QCheckBox(self.groupBox_10)
        self.checkBox_4.move(0, 80)

        self.checkBox_1 = QtGui.QCheckBox(self.groupBox_10)
        self.checkBox_1.move(100, 80)
        self.checkBox_1.toggled.connect(self.toggleGain)

        self.verticalLayout_3.addWidget(self.groupBox_10)

        self.horizontalSlider.valueChanged.connect(self.volumeChanged)

        self.retranslateUi()

    def retranslateUi(self):
        self.checkBox_4.setText("Mute")
        self.checkBox_1.setText("Allow positive gain")
        self.groupBox_10.setTitle("Volume")
        self.label_7.setText("0.0 dB")

    def enableCheck(self, enable):
        self.groupBox_10.setCheckable(enable)
        self.groupBox_10.setChecked(False)

    def getConfiguration(self):
        conf = {}
        if(not (self.groupBox_10.isCheckable() and not self.groupBox_10.isChecked())):
            conf['volume'] = {'volume': math.pow(10, self.horizontalSlider.value() / 200), 'mute': self.checkBox_4.isChecked()}
        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('volume' in conf):
                if(type(conf['volume']) == type({})):
                    if('volume' in conf['volume']):
                        if(conf['volume']['volume'] > 1):
                            self.checkBox_1.setChecked(True)
                        if(conf['volume']['volume'] == 0):
                            conf['volume']['volume'] = MIN_dB
                        self.horizontalSlider.setValue(int(200 * math.log10(conf['volume']['volume'])))
                    else:
                        self.horizontalSlider.setEnabled(False)
                    if('mute' in conf['volume']):
                        self.checkBox_4.setChecked(conf['volume']['mute'])

    def volumeChanged(self, value):
        txt = str(value / 10.0) + ' dB'
        if(value > 0):
            txt += ' (clipping)'
        self.label_7.setText(txt)

    def toggleGain(self, flag):
        if flag == True:
            self.horizontalSlider.setMaximum(200)
        else:
            self.horizontalSlider.setMaximum(0)
        #eventually show a label with warnings about clipping.
